/**
 * 工作管理 - 请假管理API
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */

import api from '../request'
import type { ApiResponse } from '@/types'

// 请假查询参数
export interface LeaveQueryParams {
  current?: number
  size?: number
  leaveType?: string | null
  status?: number | null
  applicantName?: string
  startDate?: string | null
  endDate?: string | null
  userId?: number
  propertyCompanyId?: number
  approverId?: number
  departmentId?: number
  roleType?: string
}

// 请假信息
export interface LeaveVO {
  id: number
  userId: number
  userName: string
  leaveType: string
  leaveTypeName: string
  startDate: string
  endDate: string
  startTime?: string
  endTime?: string
  days: number
  reason: string
  status: number
  statusName: string
  approverId?: number
  approverName?: string
  approveTime?: string
  approveRemark?: string
  attachmentUrl?: string
  propertyCompanyId: number
  propertyCompanyName: string
  createBy?: number
  createTime: string
  updateBy?: number
  updateTime: string
}

// 分页结果
export interface LeavePageResult {
  records: LeaveVO[]
  total: number
  size: number
  current: number
  pages: number
}

// 审批参数
export interface ApproveParams {
  approveResult: number
  approveRemark?: string
}

/**
 * 工作管理 - 请假管理API
 */
export const workLeaveApi = {
  /**
   * 分页查询请假记录
   */
  getLeavePage: (params: LeaveQueryParams) => {
    return api.get<ApiResponse<LeavePageResult>>('/api/work/leave/page', { params })
  },

  /**
   * 根据ID查询请假详情
   */
  getLeaveById: (leaveId: number) => {
    return api.get<ApiResponse<LeaveVO>>(`/api/work/leave/${leaveId}`)
  },

  /**
   * 提交请假申请
   */
  submitLeave: (data: any) => {
    return api.post<ApiResponse<number>>('/api/work/leave', data)
  },

  /**
   * 审批请假申请
   */
  approveLeave: (leaveId: number, data: ApproveParams) => {
    return api.put<ApiResponse<null>>(`/api/work/leave/${leaveId}/approve`, data)
  },

  /**
   * 撤销请假申请
   */
  cancelLeave: (leaveId: number) => {
    return api.put<ApiResponse<null>>(`/api/work/leave/${leaveId}/cancel`)
  },

  /**
   * 删除请假记录
   */
  deleteLeave: (leaveId: number) => {
    return api.delete<ApiResponse<null>>(`/api/work/leave/${leaveId}`)
  }
}

export default workLeaveApi
